/**
 * 
 */
package base.mail;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.mail.Message;
import javax.mail.search.StringTerm;

/**
 * @author bliu
 *
 */
public class SubjectRegexTerm extends StringTerm {

	protected SubjectRegexTerm(String pattern) {
		super(pattern);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public boolean match(Message msg) {
		String subj;

		try {
		    subj = msg.getSubject();
		} catch (Exception e) {
		    return false;
		}

		if (subj == null)
		    return false;

		Pattern patterner = Pattern.compile(pattern);
		Matcher matcher = patterner.matcher(subj);
		return matcher.matches();
	}

}
